module.exports = app => {
    const express = require('express');
    const router = express.Router();
    const Order = require('../model/orderUserModel')
    const Role = require('../model/rolesModel')

    // 添加订单信息
    router.post('/add',async (q,s)=>{
        try {
            console.log(q.body.address);
            let order = await Order.findOne({belong_phone:q.body.belong_phone});
            if(order == null ){
                await Order.create({
                    belong_phone:q.body.belong_phone,
                    belong_name:q.body.belong_name,
                    order_info:[
                        {
                            belong_phone:q.body.belong_phone,
                            belong_name:q.body.belong_name,
                            wastes: q.body.wastes,
                            waste_price_all: q.body.waste_price_all,
                            release_time: q.body.release_time,
                            address: q.body.address,
                            note: q.body.note,
                            start_time: q.body.start_time,
                            end_time: q.body.end_time
                        }
                    ]
                })
            }else{
                order.order_info.push({
                    belong_phone:q.body.belong_phone,
                    belong_name:q.body.belong_name,
                    wastes: q.body.wastes,
                    waste_price_all: q.body.waste_price_all,
                    release_time: q.body.release_time,
                    address: q.body.address,
                    note: q.body.note,
                    start_time: q.body.start_time,
                    end_time: q.body.end_time
                })
                order.save();
            }
            let res = await Order.findOne({belong_phone:q.body.belong_phone});
            s.send(res)
        } catch (error) {
            s.send(error);
        }
        })

    // 删除订单
    router.post('/delete',async (q,s)=>{
        let order = await Order.findOne({belong_phone:q.body.belong_phone});
        if(order != null){
            order.order_info.forEach((v,i)=>{
                if(v._id == q.body.id){
                    order.order_info.splice(i,1);
                }
            })
            order.save();
            s.send({
                data:order,
                success:"true"
            })
            } else {
                s.send({
                    data:"查无此单",
                    success:"false"
                })
            }
    })


    // 获取所有订单（所有用户）
    router.get('/getAllOrder/:phone',async (q,s)=>{
        let order = await Order.find();
        let role = await Role.find({phone:q.params.phone});
        role = role[0];
        let order_waiting=[];
        let order_ongoing=[];
        let order_finish=[];
        order.forEach((v,i)=>{
            v.order_info.forEach((vv,ii)=>{
                if (
                    (vv.address.city == role.collector.work_city && vv.address.area == role.collector.work_area)
                    || 
                    (role.collector.work_city == '' || role.collector.work_area =='' )){
                    if (vv.status == 0) {
                        if (vv.collector_phone == '') {
                            order_waiting.push(vv);
                        } else {
                            order_ongoing.push(vv);
                        }
                    } else if (vv.status == 1 || vv.status == 2){
                        order_finish.push(vv);
                    }
                }
            })
        })
        s.send([order_waiting,order_ongoing,order_finish]);
    })

    // 获取回收员所有订单
    router.get('/getCompleteOrder/:phone',async (q,s)=>{
        let order = await Order.find();
        let order_finish=[];
        order.forEach((v,i)=>{
            v.order_info.forEach((vv,ii)=>{
                if (vv.status == 1 || vv.status == 2){
                    order_finish.push(vv);
                }
            })
        })
        s.send(order_finish);
    })

    // 获取用户所有订单
    router.get('/getUserOrder/:belong_phone',async (q,s)=>{
        let order = await Order.find({belong_phone:q.params.belong_phone});
        if(order != null){
            s.send(order)
        }else{
            s.send(false);
        }
    })

    // 根据用户手机和订单id查找订单
    router.get('/find/:belong_phone/:order_id',async (q,s)=>{
        let order = await Order.find({belong_phone:q.params.belong_phone});
        let res = null;
        order.order_info.forEach((v)=>{
            if(v._id == p.params.order_id){
                res = v;
            }
        })
        s.send({
            ...res,
            belong_phone:q.params.belong_phone
        });
        })

    // 根据id查找具体订单
    router.get('/findById/:order_id',async (q,s)=>{
        let order = await Order.find();
        let res = null;
        order.forEach((v)=>{
            v.order_info.forEach(vv=>{
                if(vv._id == q.params.order_id){
                    res = {
                        ...vv,
                        belong_name:v.belong_name,
                        belong_phone:v.belong_phone,
                    };
                }
            })
        })
        s.send(res._doc);
    })

    
    // 修改物品信息
    /*
        用户层面
        1、用户修改上门时间
        2、用户修改留言（note）
        3、用户修改地址
        4、用户进行评价

        订单层面
        1、订单状态流转（status）
        2、订单生成时间，完成时间（release_time,cpmplete_time）

        回收员层面
        1、回收员接单（collector_phone,collector_name）
        2、回收员进行反馈（feedback）
        
    */
    router.post('/update',async (q,s)=>{
        let order = await Order.findOne({belong_phone:q.body.belong_phone});
        if(order != null){
            order.order_info.forEach((v)=>{
                if(v._id==q.body.order_id){
                    v.collector_phone = q.body.collector_phone == undefined ? v.collector_phone : q.body.collector_phone ;
                    v.collector_name = q.body.collector_name == undefined ? v.collector_name : q.body.collector_name ;
                    v.status = q.body.status == undefined ? v.status : q.body.status ;
                    v.release_time = q.body.release_time == undefined ? v.release_time : q.body.release_time ;
                    v.complete_time = q.body.complete_time == undefined ? v.complete_time : q.body.complete_time ;
                    v.address = q.body.address == undefined ? v.address : q.body.address ;
                    v.note = q.body.note == undefined ? v.note : q.body.note ;
                    v.start_time = q.body.start_time == undefined ? v.start_time : q.body.start_time ;
                    v.end_time = q.body.end_time == undefined ? v.end_time : q.body.end_time ;
                    v.evaluate_time = q.body.evaluate_time == undefined ? v.evaluate_time : q.body.evaluate_time ;
                    v.evaluate_content = q.body.evaluate_content == undefined ? v.evaluate_content : q.body.evaluate_content ;
                    v.evaluate_speed = q.body.evaluate_speed == undefined ? v.evaluate_speed : q.body.evaluate_speed ;
                    v.evaluate_attitude = q.body.evaluate_attitude == undefined ? v.evaluate_attitude : q.body.evaluate_attitude ;
                    v.feedback = q.body.feedback == undefined ? v.feedback : q.body.feedback;
                }
            })
            await order.save();
            s.send({
                data:order,
                success:"true"
            })
            }else{
                s.send({
                    data:"找不到!",
                    success:"false"
                })
            }
        })

    router.post('/complete',async (q,s)=>{
        let order = await Order.findOne({belong_phone:q.body.belong_phone});
        if(order != null){
            order.order_info.forEach( async (v)=>{
                if(v._id == q.body.order_id){
                    let collector = await Role.findOne({phone:v.collector_phone});
                    collector.money = collector.money - v.waste_price_all;
                    collector.credits = collector.credits + (+v.waste_price_all)*10;

                    let user = await Role.findOne({phone:v.belong_phone});
                    user.money = user.money + v.waste_price_all;
                    user.credits = user.credits + (+v.waste_price_all)*10;

                    collector.save();
                    user.save();
                    s.send("success");
                }
            })
        }
    })
    app.use('/order/user',router);
    return Order;
}